/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package gem.controller;

import gem.controller.Command;
import gem.controller.CoursesReaderWriter;
import gem.gui.courses.Common;
import gem.model.core.Course;
import gem.model.core.Department;
import gem.model.core.ScienceDomain;
import gem.model.dao.DAOFactory;

import java.util.Collection;
import javax.swing.JOptionPane;

/**
 *
 * @author Dusan
 */
public class Controller
{
    private Collection< Department > deps = null;  

    private static Controller loader = null;
    public static Controller getInstance()
    {
        if( loader == null )
        {
            loader = new Controller();
        }
        return loader;
    }

    private Controller()
    {        
    }

    public Collection< Department > listDepartments() throws java.sql.SQLException
    {
        if( deps == null )
        {
            deps = CoursesReaderWriter.readDepartments();
        }

        return deps;
    }
    
    public void execute( Command c )
    {
        System.out.println( "Command    " + c.entity + " " + c.command + " " + c.commandParam );
        switch( c.command )
        {
        case ADDNEW:
            
            break;
        case REMOVE:
            break;
        case RENAME:
            break;
        }
        
    }

    public void execute( String command, Object node )
    {
        if( command.equals( Common.COMMAND_ADD ) )
        {
            // TODO:
            // open dialog with name, and OK/CANCEL buttons
            String name;
            if( ( name = JOptionPane.showInputDialog("Unesite ime") ) != null )
            {
                if( !name.equals(""))
                {
                    System.out.println(name);
                    CoursesReaderWriter.addNew(node, name);
                }
            }
        }
        else if( command.equals( Common.COMMAND_REMOVE))
        {
            if( JOptionPane.showConfirmDialog(null, "Da li ste sigurni da želite da obrišete?") == JOptionPane.OK_OPTION )
            {
                CoursesReaderWriter.remove( node );
            }
        }
        else if( command.equals(Common.COMMAND_RENAME))
        {
            // open dialog for entering new name
            // then process it further
            String name;
            if( ( name = JOptionPane.showInputDialog("Unesite novo ime")) != null )
            {
                if( !name.equals(""))
                {
                    CoursesReaderWriter.rename(node, name);
                }
            }
            
        }
    }
}
